﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.Entity;
using DevExpress.XtraGrid.EditForm.Helpers.Controls;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.EditForm.Helpers;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Controls;

namespace HRM.DICTIONARY.UI
{
    public partial class XtraForm4 : DevExpress.XtraEditors.XtraForm
    {
        EFERP.ERP_Entities objEntity;
        BindingSource bindingsource = new BindingSource();
        BindingSource bindingsourceBranch = new BindingSource();
        DAL.ICommon<EFERP.hrm_department> exe;
        DAL.ICommon<EFERP.hrm_branch> exe2;
        bool isNew = false;

        public XtraForm4()
        {
            InitializeComponent();
            objEntity = new EFERP.ERP_Entities();
            exe = new DAL.Common<EFERP.hrm_department>(objEntity);
            exe2 = new DAL.Common<EFERP.hrm_branch>(objEntity);

            bindingsource.DataSource = objEntity.hrm_departments.Local.ToBindingList();
            gridControl1.DataSource = bindingsource;


            //exe.AddRepositoryGridLookupEdit(repositoryItemGridLookUpEdit1, "hrm_branch.branchname", "hrm_branch.branchcode");
            exe2.AddRepositoryGridLookupEdit(repositoryItemGridLookUpEdit1, "branchname", "branchcode");
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            bindingsource.AddNew();
            gridView1.SetRowCellValue(gridView1.FocusedRowHandle, "departmentcode", exe.NewID("11111"));
            isNew = true;
            gridView1.ShowEditForm();
        }

        private void gridView1_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            try
            {
                EFERP.hrm_department obj = (EFERP.hrm_department)e.Row;
                var ids = objEntity.hrm_departments.Where(x => x.departmentcode == obj.departmentcode).ToList();
                if (isNew)
                {
                    if (ids.Count == 0)
                    {
                        objEntity.SaveChanges();
                    }
                    else
                    {
                        MessageBox.Show("Vui lòng không làm cùng một thằng, Gay vãi! (Duplicate Primary Key)");
                    }

                    isNew = false;
                }

                else
                {
                    objEntity.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
            } 
        }

        private void gridView1_ShowingPopupEditForm(object sender, DevExpress.XtraGrid.Views.Grid.ShowingPopupEditFormEventArgs e)
        {
            foreach (Control control in e.EditForm.Controls)
            {
                if (!(control is EditFormContainer))
                {
                    continue;
                }
                foreach (Control nestedControl in control.Controls)
                {
                    if (!(nestedControl is PanelControl))
                    {
                        continue;
                    }
                    foreach (Control button in nestedControl.Controls)
                    {
                        if (button is SimpleButton)
                        {
                            var simpleButton = button as SimpleButton;
                            ChangeButtonCaption(simpleButton);
                            ChangeButtonSize(simpleButton);
                        }
                    }
                }
            }
        }

        private static void ChangeButtonCaption(SimpleButton btn)
        {
            var newUpdateBtnText = "Cập Nhật";
            var newCancelBtnText = "Hủy Bỏ";
            var btnText = btn.Text;
            switch (btnText)
            {
                case "Update":
                    btn.Text = newUpdateBtnText;
                    break;
                case "Cancel":
                    btn.Text = newCancelBtnText;
                    break;
            }
        }
        private static void ChangeButtonSize(SimpleButton btn)
        {
            btn.CalcBestSize();
        }

        

        private void gridView1_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e)
        {

            // Dùng  cho trường hợp Edit mode = Edit Form
            EditFormValidateEditorEventArgs agrs = (EditFormValidateEditorEventArgs)e;

            if (agrs.Value == "" || agrs.Value == null)
            {
                e.Valid = false;
                e.ErrorText = agrs.Column.Caption + " không được rỗng !";
            }

            //// Dùng  cho trường hợp Edit mode = Inplace
            //GridView view = sender as GridView;
            //if (view.FocusedColumn.FieldName == "professionalname")
            //{
            //    if (e.Value == "" || e.Value == null)
            //    {
            //        e.Valid = false;
            //        e.ErrorText = "Rỗng";
            //    }
            //}
        }

        private void gridView1_InvalidValueException(object sender, DevExpress.XtraEditors.Controls.InvalidValueExceptionEventArgs e)
        {
            //Do not perform any default action 
            e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
            //Show the message with the error text specified 
            MessageBox.Show(e.ErrorText);

        }

        private void gridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            //GridView view = sender as GridView;
            //GridColumn col1 = view.Columns["professionalname"];
            //GridColumn col2 = view.Columns["description"];
            ////Get the value of the first column
            //string strCol1 = (string)view.GetRowCellValue(e.RowHandle, col1);
            ////Get the value of the second column
            //string strCol2 = (string)view.GetRowCellValue(e.RowHandle, col2);
            ////Validity criterion
            //if (strCol1 == "" || strCol1 == null)
            //{
            //    e.Valid = false;
            //    //Set errors with specific descriptions for the columns
            //    view.SetColumnError(col1, "Chuyên môn mà rỗng hả mại?");
            //}

            //if (strCol2 == "" || strCol2 == null)
            //{
            //    e.Valid = false;
            //    //Set errors with specific descriptions for the columns
            //    view.SetColumnError(col2, "Lẽ ra tao rỗng được, tại test thôi");
            //}
        }

        private void gridView1_InvalidRowException(object sender, DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e)
        {
            //Suppress displaying the error message box
            e.ExceptionMode = ExceptionMode.NoAction;
        }

        private void repositoryItemGridLookUpEdit1_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            if (e.Button.Kind != ButtonPredefines.Plus) return;
            xucBranchAdd add = new xucBranchAdd(exe2);
            add.ShowDialog();
        }
    }
}